################################################################################
# Run this file to produce all figures in 
# "Multidimensional Inequality Measurement via Optimal Transport" 
# by Fan, Henry, Pass, Rivero

# Contact: Jorge A Rivero, riverj11@rpi.edu, https://www.jarivero.com/

# Notes: -Need R version 4.0.5 (for Rgeogram package, see readme).
#        -Need the associated Rtools 4.0 version.
#        -Binary versions of packages are used since they are compatible with R 4.0.5.
#        -Windows is strongly recommended, other systems were not tested. 
#        -The entire program may take several days to run.
# Please see readme for more details.

################################################################################

# Set your working directory to source file 
# Seed is 102112516, check individual files

# install packages, load them, load functions
source("functions/package_installer.R")

#### Section 1 Figures ####
source("figures/fig1.R") # Fig 1
rm(list = setdiff(ls(), lsf.str()))
gc()

#### Section 2 Figures ####
source("figures/fig2.R") # Fig 2
rm(list = setdiff(ls(), lsf.str()))
gc()

source("figures/fig3.R") # Fig 3
rm(list = setdiff(ls(), lsf.str()))
gc()

source("figures/fig4.R") # Fig 4
rm(list = setdiff(ls(), lsf.str()))
gc()

#### Section 3 Figures ####
source("functions/create_dataset.R") # Compile all SCF yearly data into one csv file
source("functions/separate_imputes.R") # Separate the 5 imputes to process

# Fig 5,6 and 7
source("functions/aggregate_inequality.R") # Calculate ILFs, Shares, Gini for each year-impute combination
source("figures/fig5.R") # Fig 5
source("figures/fig6.R") # Fig 6
source("figures/fig7.R") # Fig 7

# Fig 7 and 8
source("functions/grouped_inequality.R") # Calculate ILFs, Shares, Gini for each year-impute combination
source("figures/fig8.R") # Fig 8


